<script src="lib/protobuf.js"></script>

<script>
  let model;

  async function loadBinaryDataFromUrl(url) {
    const req = new Request(url);
    const res = await fetch(req);
    if (!res.ok) {
      throw Error(res.statusText);
    }
    return res.arrayBuffer();
  }

  async function loadTextDataFromUrl(url) {
    const req = new Request(url);
    const res = await fetch(req);
    if (!res.ok) {
      throw Error(res.statusText);
    }
    return res.text();
  }

  // caffe
  (async function(){

    const bundle = await loadTextDataFromUrl('proto/caffe.json');
    const root = protobuf.Root.fromJSON(JSON.parse(bundle));
    const netDef = root.nested.caffe.NetParameter;

    const url = "data/bvlc_googlenet.caffemodel";

    try {
      const data = await loadBinaryDataFromUrl(url);
      const graph = netDef.decode(new Uint8Array(data));
      model = graph;
      console.log(graph);
    }
    catch (e) {
      console.error('Download the model from `http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel` and place it under `data/bvlc_googlenet.caffemodel`');
    }
  }());

</script>